﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MayhemCore;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Runtime.Serialization;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using System.Collections.ObjectModel;

namespace MayhemPowerShell
{
    [DataContract]
    [MayhemModule("PowerShell Script", "Execute PowerShell Script")]
    public class PowerShellCommand : ReactionBase, IWpfConfigurable
    {
        //[DataMember]
        //private Command command;

        [DataMember]
        private String ScriptText;

        [DataMember]
        private bool UseFile;

        [DataMember]
        private String FilePath;

        public override void Perform()
        {
            try
            {
                InitialSessionState sessionState = InitialSessionState.CreateDefault();
                using (  PowerShell shell = PowerShell.Create())
                {
                    if (UseFile)
                    {
                        shell.ExecuteScript(String.Format("Powershell.exe -noprofile -executionpolicy bypass -file \"{0}\"", FilePath));
                    }
                    else
                    {
                        shell.ExecuteScript("Set-ExecutionPolicy RemoteSigned -Scope process");
                        shell.ExecuteScript("$env:path      = [system.environment]::getenvironmentvariable(\"path\")");
                        shell.ExecuteScript(ScriptText);
                    }
                }
            }
            catch
            {
                ErrorLog.AddError(ErrorType.Failure, "PowerShell Command failed to execute.");
                return;
            }
        }

        public string GetConfigString()
        {
            return string.Format("Execute PowerShell Command: {0}", ScriptText);
        }

        protected override void OnLoadDefaults()
        {
            ScriptText = "";
            UseFile = false;
            FilePath = "";
        }

        public WpfConfiguration ConfigurationControl
        {
            get { return new ScriptConfig(ScriptText, UseFile, FilePath); }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            ScriptConfig config = (ScriptConfig)configurationControl;
            UseFile = config.UseFile;

            if (UseFile)
            {
                FilePath = config.FilePath;
            }
            else
            {
                ScriptText = config.ScriptText;
            }

            

            //CommandConfig config = (CommandConfig)configurationControl;
            //command = new Command(config.Command.Name);
            //var enumerator = config.Parameters.GetEnumerator();
            //while (enumerator.Current != null)
            //{
            //    command.Parameters.Add(new CommandParameter(
            //}
        }
    }

    
}
